package crawler;

import restclient.RESTClient;
import parser.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.ArrayList;
import java.util.List;

import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PushbackInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


/**
 *
 * @author Ilio Catallo, Eleonora Ciceri
 * @version: 1.0
 * 
 * Description: The class implements a web crawler capable of retrieving
 *              photos from a given web service (e.g. Google Images)
 * 
 * Date: Nov 16, 2011
 */
public abstract class PhotoCrawler extends Crawler{
    
    // Attributes
    private String path;
    
    /**
     * Constructor for the class PhotoCrawler
     * @param type
     * @param query
     * @param attributes
     * @param conditionAttribute
     * @param path 
     */
    public PhotoCrawler(String protocol, LinkedHashMap<String, String> query, String path) {

        super(protocol, query);
        this.path = path;
    }
    
    
    @Override
    protected List<Object> preProcessData(List<HashMap<String, String>> responseAttributes) {
        // Variables 
        List<String> URLs = composeURLs(responseAttributes);
        List<Object> data = new ArrayList<Object>();
        
        // We need something better to cat from List<String> to List<Object>
        for (Object URL : URLs) 
            data.add(URL);

        return data;
    }
    
    @Override
    protected ArrayList<Object> processData(List<Object> data) {

        RESTClient restClient = new RESTClient("http");
        
        
        for (int i = 0; i < data.size(); i++){
            
            restClient.executeQuery((String)data.get(i));
            PushbackInputStream photo = new PushbackInputStream(restClient.getResponseContent());

            try {
                
                int availBytes = photo.available();
                
                // Check if the image is empty (check if 214 is constant for all the images)
                if (availBytes != 214)
                    savePhotoToDisk(photo, Integer.toString(i));
                
            } catch (IOException e) {
                System.err.println("Unable to verify if the image is empty");
                e.printStackTrace();
            }
            
            //if (photo != null)
              //  savePhotoToDisk(photo, Integer.toString(i));
        }
        
        restClient.closeConnection();
        return null;
    }
    
    protected abstract List<String> composeURLs(List<HashMap<String, String>> resposeAttributes);
    
     private void savePhotoToDisk(InputStream photo, String fileName){
        
        try {
            String completePath = this.path + "/" + fileName + ".jpg";
            File photoFile = new File(completePath);
            int len;

            OutputStream out = new FileOutputStream(photoFile);
            byte buf[] = new byte[1024];

            while((len = photo.read(buf)) > 0)
                out.write(buf, 0, len);

            out.close();
            photo.close();
        }
        catch (IOException e) {
            
        }
    
        
    }
    
}
